package com.dragonwu.controller;

import com.dragonwu.domain.Order;
import com.dragonwu.feign.StockServiceFeign;
import com.dragonwu.mapper.OrderMapper;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;

/**
 * @author DragonWu
 * @date 2022-10-02 18:17
 **/
@RestController
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private StockServiceFeign stockServiceFeign;

    @PostMapping("/add")
    @Transactional
    public String add(@RequestBody Order order) {
        orderMapper.insert(order);
        return "ok";
    }

    @GlobalTransactional
    @PutMapping("/update")
    public String update(String name) {
        Order order = new Order();
        order.setId(123456L);
        order.setName(name);
        order.setPrice(BigDecimal.valueOf(1999));
        orderMapper.updateById(order);
        stockServiceFeign.update();
        int x = 1 / 0;
        return "ok";
    }

}
